Trò chơi đua xe động vật trong UNITY Engine
114.620 lượt xem;
- UpgradeLayer.cs
- ShopScreen /
- AnimalRacing /
- Scripts /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4 public class UpgradeLayer : MonoBehaviour {
5
6 public Label currentLevelLabel;
7 public Label nextLevelLabel;
8 public Label goldLabel;
9 public Label maxLevelLabel;
10
11 public GameObject nextLevel;
12 public GameObject boardGold;
13 public GameObject upgradeButton;
14
15 public GameObject chooseSkill;
16
17 public GameObject[] levelIconItems;
18
19 public Sprite[] levelIcons;
20
21 public ShopScreen shopScreen;
22
23 private UpgradeInfo upgradeInfo;
24
25 private string[] itemNames;
26
27 private int itemIndex;
28
29 public void Start () {
30
31 }
32
33 void Update () {
34
35 }
36
37 public void setFont(BitmapFont fontShop)
38 {
39 currentLevelLabel.setFont(fontShop);
40 nextLevelLabel.setFont(fontShop);
41 maxLevelLabel.setFont(fontShop);
42 goldLabel.setFont(fontShop);
43
44 maxLevelLabel.setText("MAX LEVEL", 0, 15);
45 itemNames = new string[] {"Speed","Jump" };
46 }
47
48 public void setUpgradeInfo(UpgradeInfo upgradeInfo)
49 {
50 this.upgradeInfo = upgradeInfo;
51 }
52
53
54 public void UpdateUI(int itemIndex)
55 {
56 if (upgradeInfo == null) return;
57 this.itemIndex = itemIndex;
58 if (upgradeInfo.getLevel(Attr.currentAnimal, itemIndex) < 5)
59 {
60 currentLevelLabel.gameObject.SetActive(true);
61 nextLevelLabel.gameObject.SetActive(true);
62 goldLabel.gameObject.SetActive(true);
63 nextLevel.SetActive(true);
64 maxLevelLabel.gameObject.SetActive(false);
65 boardGold.SetActive(true);
66 upgradeButton.SetActive(true);
67
68 currentLevelLabel.setText(itemNames[itemIndex] + " Lv" + upgradeInfo.getLevel(Attr.currentAnimal, itemIndex) + " : " + upgradeInfo.getItem(Attr.currentAnimal, itemIndex, false), 0, 12);
69 nextLevelLabel.setText(itemNames[itemIndex] + " Lv" + (upgradeInfo.getLevel(Attr.currentAnimal, itemIndex) + 1) + " : " + upgradeInfo.getItem(Attr.currentAnimal, itemIndex, true), 0, 12);
70 goldLabel.setText("" + upgradeInfo.getCostItem(Attr.currentAnimal, itemIndex), 0, 0);
71
72 }
73 else
74 {
75 currentLevelLabel.gameObject.SetActive(false);
76 nextLevelLabel.gameObject.SetActive(false);
77 goldLabel.gameObject.SetActive(false);
78 nextLevel.SetActive(false);
79 maxLevelLabel.gameObject.SetActive(true);
80 boardGold.SetActive(false);
81 upgradeButton.SetActive(false);
82 }
83
84 levelIconItems[itemIndex].GetComponent<SpriteRenderer>().sprite = levelIcons[upgradeInfo.getLevel(Attr.currentAnimal, itemIndex) - 1];
85 }
86
87 public void upgrade()
88 {
89 if (upgradeInfo.getCostItem(Attr.currentAnimal, itemIndex) > Data.getData(Data.KEY_COIN))
90 {
91 shopScreen.showNotEnoughCoin();
92 }
93 else
94 {
95 Data.saveData(Data.KEY_COIN, Data.getData(Data.KEY_COIN) - upgradeInfo.getCostItem(Attr.currentAnimal, itemIndex));
96 upgradeInfo.upgrade(Attr.currentAnimal, itemIndex);
97 UpdateUI(itemIndex);
98 shopScreen.UpdateCoin();
99 }
100 }
101
102 public void resetChooseItem()
103 {
104 chooseSkill.transform.localPosition = new Vector3(1, 0.66f, chooseSkill.transform.localPosition.z);
105 }
106 }
2 using System.Collections;
3
4 public class UpgradeLayer : MonoBehaviour {
5
6 public Label currentLevelLabel;
7 public Label nextLevelLabel;
8 public Label goldLabel;
9 public Label maxLevelLabel;
10
11 public GameObject nextLevel;
12 public GameObject boardGold;
13 public GameObject upgradeButton;
14
15 public GameObject chooseSkill;
16
17 public GameObject[] levelIconItems;
18
19 public Sprite[] levelIcons;
20
21 public ShopScreen shopScreen;
22
23 private UpgradeInfo upgradeInfo;
24
25 private string[] itemNames;
26
27 private int itemIndex;
28
29 public void Start () {
30
31 }
32
33 void Update () {
34
35 }
36
37 public void setFont(BitmapFont fontShop)
38 {
39 currentLevelLabel.setFont(fontShop);
40 nextLevelLabel.setFont(fontShop);
41 maxLevelLabel.setFont(fontShop);
42 goldLabel.setFont(fontShop);
43
44 maxLevelLabel.setText("MAX LEVEL", 0, 15);
45 itemNames = new string[] {"Speed","Jump" };
46 }
47
48 public void setUpgradeInfo(UpgradeInfo upgradeInfo)
49 {
50 this.upgradeInfo = upgradeInfo;
51 }
52
53
54 public void UpdateUI(int itemIndex)
55 {
56 if (upgradeInfo == null) return;
57 this.itemIndex = itemIndex;
58 if (upgradeInfo.getLevel(Attr.currentAnimal, itemIndex) < 5)
59 {
60 currentLevelLabel.gameObject.SetActive(true);
61 nextLevelLabel.gameObject.SetActive(true);
62 goldLabel.gameObject.SetActive(true);
63 nextLevel.SetActive(true);
64 maxLevelLabel.gameObject.SetActive(false);
65 boardGold.SetActive(true);
66 upgradeButton.SetActive(true);
67
68 currentLevelLabel.setText(itemNames[itemIndex] + " Lv" + upgradeInfo.getLevel(Attr.currentAnimal, itemIndex) + " : " + upgradeInfo.getItem(Attr.currentAnimal, itemIndex, false), 0, 12);
69 nextLevelLabel.setText(itemNames[itemIndex] + " Lv" + (upgradeInfo.getLevel(Attr.currentAnimal, itemIndex) + 1) + " : " + upgradeInfo.getItem(Attr.currentAnimal, itemIndex, true), 0, 12);
70 goldLabel.setText("" + upgradeInfo.getCostItem(Attr.currentAnimal, itemIndex), 0, 0);
71
72 }
73 else
74 {
75 currentLevelLabel.gameObject.SetActive(false);
76 nextLevelLabel.gameObject.SetActive(false);
77 goldLabel.gameObject.SetActive(false);
78 nextLevel.SetActive(false);
79 maxLevelLabel.gameObject.SetActive(true);
80 boardGold.SetActive(false);
81 upgradeButton.SetActive(false);
82 }
83
84 levelIconItems[itemIndex].GetComponent<SpriteRenderer>().sprite = levelIcons[upgradeInfo.getLevel(Attr.currentAnimal, itemIndex) - 1];
85 }
86
87 public void upgrade()
88 {
89 if (upgradeInfo.getCostItem(Attr.currentAnimal, itemIndex) > Data.getData(Data.KEY_COIN))
90 {
91 shopScreen.showNotEnoughCoin();
92 }
93 else
94 {
95 Data.saveData(Data.KEY_COIN, Data.getData(Data.KEY_COIN) - upgradeInfo.getCostItem(Attr.currentAnimal, itemIndex));
96 upgradeInfo.upgrade(Attr.currentAnimal, itemIndex);
97 UpdateUI(itemIndex);
98 shopScreen.UpdateCoin();
99 }
100 }
101
102 public void resetChooseItem()
103 {
104 chooseSkill.transform.localPosition = new Vector3(1, 0.66f, chooseSkill.transform.localPosition.z);
105 }
106 }